using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The EmployeeSalaryTransTableDB class is responsible for interacting with the database to retrieve and store information
/// about EmployeeSalaryTransTable objects.
/// </summary>
public class EmployeeSalaryTransTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of EmployeeSalaryTransTable from the underlying datasource.
/// </summary>
/// <param name="employeeSalaryTransKey">The unique EmployeeSalaryTransKey of the EmployeeSalaryTransTable in the database.</param>
/// <returns>An EmployeeSalaryTransTable when the EmployeeSalaryTransKey was found in the database, or null otherwise.</returns>
public static EmployeeSalaryTransTable GetItem(long employeeSalaryTransKey)
{
EmployeeSalaryTransTable myEmployeeSalaryTransTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTransTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@employeeSalaryTransKey", employeeSalaryTransKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myEmployeeSalaryTransTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myEmployeeSalaryTransTable;
}
}

/// <summary>
/// Returns a list with EmployeeSalaryTransTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeSalaryTransTable objects.</returns>
public static EmployeeSalaryTransTableList GetList(int SchoolKey)
{
EmployeeSalaryTransTableList tempList = new EmployeeSalaryTransTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTransTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@SchoolKey", SchoolKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with EmployeeSalaryTransTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeSalaryTransTable objects.</returns>
public static EmployeeSalaryTransTableList GetListByCurrencyCode(short CurrencyCode,int SchoolKey)
{
EmployeeSalaryTransTableList tempList = new EmployeeSalaryTransTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTransTableSelectListByCurrencyCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@currencyCode", CurrencyCode);
myCommand.Parameters.AddWithValue("@SchoolKey", SchoolKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with EmployeeSalaryTransTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeSalaryTransTable objects.</returns>
public static EmployeeSalaryTransTableList GetListByPaymentTypeCode(short PaymentTypeCode,int SchoolKey)
{
EmployeeSalaryTransTableList tempList = new EmployeeSalaryTransTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTransTableSelectListByPaymentTypeCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@paymentTypeCode", PaymentTypeCode);
myCommand.Parameters.AddWithValue("@SchoolKey", SchoolKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with EmployeeSalaryTransTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeSalaryTransTable objects.</returns>
public static EmployeeSalaryTransTableList GetListByUserAccountKey(int UserAccountKey,int SchoolKey)
{
EmployeeSalaryTransTableList tempList = new EmployeeSalaryTransTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTransTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myCommand.Parameters.AddWithValue("@SchoolKey", SchoolKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a EmployeeSalaryTransTable in the database.
/// </summary>
/// <param name="myEmployeeSalaryTransTable">The EmployeeSalaryTransTable instance to save.</param>
/// <returns>The new EmployeeSalaryTransKey if the EmployeeSalaryTransTable is new in the database or the existing EmployeeSalaryTransKey when an item was updated.</returns>
public static long Save(EmployeeSalaryTransTable myEmployeeSalaryTransTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTransTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myEmployeeSalaryTransTable.EmployeeSalaryTransKey == -1)
{myCommand.Parameters.AddWithValue("@employeeSalaryTransKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@employeeSalaryTransKey", myEmployeeSalaryTransTable.EmployeeSalaryTransKey);
}
if (myEmployeeSalaryTransTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myEmployeeSalaryTransTable.UserAccountKey);
}
if (myEmployeeSalaryTransTable.PaymentTypeCode == null){
myCommand.Parameters.AddWithValue("@paymentTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@paymentTypeCode", myEmployeeSalaryTransTable.PaymentTypeCode);
}
if (myEmployeeSalaryTransTable.CurrencyCode == null){
myCommand.Parameters.AddWithValue("@currencyCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@currencyCode", myEmployeeSalaryTransTable.CurrencyCode);
}
if (myEmployeeSalaryTransTable.TransactionDate == null){
myCommand.Parameters.AddWithValue("@transactionDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@transactionDate", myEmployeeSalaryTransTable.TransactionDate);
}
if (myEmployeeSalaryTransTable.Amount == null){
myCommand.Parameters.AddWithValue("@amount", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@amount", myEmployeeSalaryTransTable.Amount);
}
if (myEmployeeSalaryTransTable.AdditionalAmount == null){
myCommand.Parameters.AddWithValue("@additionalAmount", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@additionalAmount", myEmployeeSalaryTransTable.AdditionalAmount);
}
if (myEmployeeSalaryTransTable.OthersAmount == null){
myCommand.Parameters.AddWithValue("@othersAmount", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@othersAmount", myEmployeeSalaryTransTable.OthersAmount);
}
if (myEmployeeSalaryTransTable.Remarks == null){
myCommand.Parameters.AddWithValue("@remarks", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remarks", myEmployeeSalaryTransTable.Remarks);
}
if (myEmployeeSalaryTransTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myEmployeeSalaryTransTable.Status);
}
if (myEmployeeSalaryTransTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myEmployeeSalaryTransTable.CreatedDateTime);
}
if (myEmployeeSalaryTransTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myEmployeeSalaryTransTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a EmployeeSalaryTransTable from the database.
/// </summary>
/// <param name="employeeSalaryTransKey">The EmployeeSalaryTransKey of the EmployeeSalaryTransTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(long employeeSalaryTransKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTransTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@employeeSalaryTransKey", employeeSalaryTransKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the EmployeeSalaryTransTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static EmployeeSalaryTransTable FillDataRecord(IDataRecord myDataRecord )
{
EmployeeSalaryTransTable myEmployeeSalaryTransTable = new EmployeeSalaryTransTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmployeeSalaryTransKey")))
{
myEmployeeSalaryTransTable.EmployeeSalaryTransKey = myDataRecord.GetInt64(myDataRecord.GetOrdinal("EmployeeSalaryTransKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myEmployeeSalaryTransTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentTypeCode")))
{
myEmployeeSalaryTransTable.PaymentTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("PaymentTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CurrencyCode")))
{
myEmployeeSalaryTransTable.CurrencyCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CurrencyCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TransactionDate")))
{
myEmployeeSalaryTransTable.TransactionDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("TransactionDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Amount")))
{
myEmployeeSalaryTransTable.Amount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Amount"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AdditionalAmount")))
{
myEmployeeSalaryTransTable.AdditionalAmount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("AdditionalAmount"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OthersAmount")))
{
myEmployeeSalaryTransTable.OthersAmount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("OthersAmount"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remarks")))
{
myEmployeeSalaryTransTable.Remarks = myDataRecord.GetString(myDataRecord.GetOrdinal("Remarks"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myEmployeeSalaryTransTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myEmployeeSalaryTransTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
    myEmployeeSalaryTransTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FirstName")))
{
    myEmployeeSalaryTransTable.FirstName = myDataRecord.GetString(myDataRecord.GetOrdinal("FirstName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LastName")))
{
    myEmployeeSalaryTransTable.LastName = myDataRecord.GetString(myDataRecord.GetOrdinal("LastName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherName")))
{
    myEmployeeSalaryTransTable.OtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DateOfBirth")))
{
    myEmployeeSalaryTransTable.DateOfBirth = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("DateOfBirth"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("NRICNumber")))
{
    myEmployeeSalaryTransTable.NRICNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("NRICNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
{
    myEmployeeSalaryTransTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
}
return myEmployeeSalaryTransTable;
}
}
